1879B - Chips on the Board - CodeForces Solution


greedy

Please click on ads to support us..

Python Code:

def solve():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    a.sort()
    b.sort()
    sum1 = n * b[0]
    sum2 = n * a[0]

    for i in range(n):
        sum1 += a[i]
        sum2 += b[i]

    print(min(sum1, sum2))

def main():
    t = int(input())
    for _ in range(t):
        solve()

if __name__ == "__main__":
    main()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main()
{

    int t;
    cin >> t;
    while (t--)
    {

        int n;
        cin >> n;

        vector<int> a;
        vector<int> b;

        for (int i = 0; i < n; i++)
        {
            int x;
            cin >> x;
            a.push_back(x);
        }
        for (int i = 0; i < n; i++)
        {
            int x;
            cin >> x;
            b.push_back(x);
        }
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());
        long long h = 0;
        for (int i = 0; i < n; i++)
        {
            h += a[0] + b[i];
        }
        long long ans = 0;
        for (int i = 0; i < n; i++)
        {
            ans += b[0] + a[i];
        }
        cout << min(h, ans) << "\n";
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies